import 'package:flutter/services.dart';
import 'package:ball_lion/server/api/other_api.dart';
import 'package:ball_lion/utils/sp_utils.dart';
import 'package:yaml/yaml.dart';

import 'package:ball_lion/entity/other/resource_entity.dart';

class AppConfig {
  static AppConfig? _config;

  static AppConfig get config => _config!;

  static init() async {
    final content =
        await rootBundle.loadString("android/app/src/main/assets/config.yaml");
    YamlMap json = loadYaml(content)['config'];
    _config = AppConfig.fromJson(json.cast());
  }

  String? channel;
  late bool isDebug;
  List<ResourceEntity>? limit;

  AppConfig.fromJson(Json json) {
    channel = json['channel'];
    isDebug = json['isDebug'] ?? false;
  }

  Future getLimit() async {
    limit = await OtherApi.getAppList("channel_config") ?? [];
  }

  String get channelType {
    switch (channel) {
      case "c11001":
        return "华为";
      case "c11002":
        return "小米";
      case "c11003":
        return "oppo";
      case "c11004":
        return "vivo";
      case "c11005":
        return "魅族";
      case "c11006":
        return "应用宝";
      case "c11007":
        return "百度";
      case "c11008":
        return "360";
      case "c11009":
        return "阿里";
      case "c11010":
        return "百度推广";
      default:
        return "$channel";
    }
  }

  bool get showOdds {
    List<ResourceEntity>? entity =
        limit?.where((element) => element.button == "odds").toList();
    if (entity?.isNotEmpty == true) {
      return !(entity?[0].content?.split(",").contains(channel) ?? false);
    } else {
      return true;
    }
  }

  bool get showRelative {
    List<ResourceEntity>? entity =
        limit?.where((element) => element.button == "tuijian").toList();
    if (entity?.isNotEmpty == true) {
      return !(entity?[0].content?.split(",").contains(channel) ?? false);
    } else {
      return true;
    }
  }

  List<bool> get showNavigation {
    List<bool> result = [true, true, true, true, true];
    for (var i = 0; i < 5; i++) {
      List<ResourceEntity> list = limit
              ?.where((element) => element.button == "navigation_$i")
              .toList() ??
          [];
      if (list.isNotEmpty) {
        result[i] = !(list[0].content?.split(",").contains(channel) ?? false);
      }
    }
    return result;
  }
}
